package com.dsit.sq.mydefineview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.dsit.sq.R;

/**
 * Created by dsit_dev04 on 2016/10/19.
 */
public class CircleProgressView extends View {
    private Paint colorpaint;
    private Paint propaint;

    private int cx, cy;
    private int width, height;
    private int whsize;
    private Bitmap iconBitmap;

    public CircleProgressView(Context context) {
        super(context);
        init();
    }

    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        colorpaint = new Paint();
        propaint = new Paint();

        colorpaint.setAntiAlias(true);
        colorpaint.setColor(getResources().getColor(R.color.color_gray));
        colorpaint.setStyle(Paint.Style.STROKE);
        colorpaint.setStrokeWidth(5);
        colorpaint.setStrokeCap(Paint.Cap.ROUND);

        propaint.setAntiAlias(true);
        propaint.setColor(getResources().getColor(progressColorId));
        propaint.setStyle(Paint.Style.STROKE);
        propaint.setStrokeWidth(5);
        propaint.setStrokeCap(Paint.Cap.ROUND);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawarc(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        width = w;
        height = h;
        whsize = w > h ? h : w;
        cx = width / 2;
        cy = height / 2;
        //getsize();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public int progress = 0;
    public int progressColorId = R.color.colorOrange;
    public int seekIconId = R.mipmap.bg_light_yellow_03;
    public boolean isClockWise = true;
    public int startAngel = 158;
    public int totalAngel = 224;

    private void drawarc(Canvas canvas) {
        iconBitmap = BitmapFactory.decodeResource(getResources(), seekIconId);
        propaint.setColor(getResources().getColor(progressColorId));
        int imageWidth;
        RectF oval = new RectF(Dp2Px(getContext(), 15),
                Dp2Px(getContext(), 15), whsize - Dp2Px(getContext(), 15),
                whsize - Dp2Px(getContext(), 15));
        float angle;
        if (isClockWise){
            canvas.drawArc(oval, startAngel, totalAngel, false, colorpaint);
            canvas.drawArc(oval, (float) startAngel, (float) (totalAngel / 100) * progress + (float) 0.01, false, propaint);
            angle = (float) ((startAngel + (totalAngel / 100) * progress) * (Math.PI / 180));
            imageWidth = whsize / 4;
        }else{
            canvas.drawArc(oval, startAngel, -totalAngel, false, colorpaint);
            canvas.drawArc(oval, (float) startAngel, -(float) (totalAngel / 100) * progress + (float) 0.01, false, propaint);
            angle = (float) ((startAngel - (totalAngel / 100) * progress) * (Math.PI / 180));
            imageWidth = whsize / 6;
        }
        float r = whsize / 2 - Dp2Px(getContext(), 15);
        float x = (float) (cx + r * Math.cos(angle));
        float y = (float) (cy + r * Math.sin(angle));
        RectF seekIconPosition = new RectF(x - imageWidth / 2, y - imageWidth / 2, x + imageWidth / 2, y + imageWidth / 2);
        canvas.drawBitmap(iconBitmap, null, seekIconPosition, null);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public float Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (dp * scale + 0.5f);
    }

    public float Px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (px / scale + 0.5f);
    }
}
